package com.zp.base.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.base.entity.bo.ClinicIndexBo;
import com.zp.base.entity.vo.ClinicIndexVo;
import com.zp.base.service.IClinicIndexService;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 号别控制器
 * 前端访问路由地址为:/base/clinicIndex
 *
 * @author zhang peng
 * @date 2023-01-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/clinicIndex")
public class ClinicIndexController extends BaseController {

    private final IClinicIndexService iClinicIndexService;

    /**
     * 查询号别列表 (分页)
     */
    @SaCheckPermission("base:clinicIndex:list")
    @GetMapping("/selectPage")
    public TableDataInfo<ClinicIndexVo> selectPage (ClinicIndexBo bo, PageQuery pageQuery) {
        return iClinicIndexService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询号别列表 (不分页)
     */
    @SaCheckPermission("base:clinicIndex:list")
    @GetMapping("/list")
    public R<List<ClinicIndexVo>> list (ClinicIndexBo bo) {
        List<ClinicIndexVo> list = iClinicIndexService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出号别列表
     */
    @SaCheckPermission("base:clinicIndex:export")
    @Log(title = "号别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export (ClinicIndexBo bo, HttpServletResponse response) {
        List<ClinicIndexVo> list = iClinicIndexService.queryList(bo);
        ExcelUtil.exportExcel(list, "号别", ClinicIndexVo.class, response);
    }

    /**
     * 获取号别详细信息
     *
     * @param clinicIndexId 主键
     */
    @SaCheckPermission("base:clinicIndex:query")
    @GetMapping("/{clinicIndexId}")
    public R<ClinicIndexVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String clinicIndexId) {
        return R.ok(iClinicIndexService.queryById(clinicIndexId));
    }

    /**
     * 新增号别
     */
    @SaCheckPermission("base:clinicIndex:add")
    @Log(title = "号别", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody ClinicIndexBo bo) {
        return toAjax(iClinicIndexService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改号别
     */
    @SaCheckPermission("base:clinicIndex:edit")
    @Log(title = "号别", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody ClinicIndexBo bo) {
        return toAjax(iClinicIndexService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除号别
     *
     * @param clinicIndexIds 主键串
     */
    @SaCheckPermission("base:clinicIndex:remove")
    @Log(title = "号别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{clinicIndexIds}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] clinicIndexIds) {
        return toAjax(iClinicIndexService.deleteWithValidByIds(Arrays.asList(clinicIndexIds), true) ? 1 : 0);
    }
}
